package com.gxa.service.impl;

import com.gxa.common.R;
import com.gxa.mapper.PurchaseMapper;
import com.gxa.pojo.Goods_Save;
import com.gxa.pojo.Purchase;
import com.gxa.pojo.Receipt_Purchase;
import com.gxa.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
@Service
public class PurchaseServiceImpl implements PurchaseService{
    R r=new R();
    @Autowired(required = false)
    PurchaseMapper purchaseMapper;

    @Override
    public R inerstReceiptPurchase(Receipt_Purchase receipt_Purchase) {
        List<Purchase> list = receipt_Purchase.getPurchaseList();

        Integer result1 = purchaseMapper.inerstReceiptPurchase(receipt_Purchase);
        Integer result2 = purchaseMapper.inerstPurchase(list);


        for (Purchase item :list) {
            if("2".equals(receipt_Purchase.getPurchase_Business_Type())){
                item.setPurchase_Number(0 - item.getPurchase_Number());
            }
            Goods_Save result3 = purchaseMapper.queryGoodsSave(item.getWarehouse_Id(),item.getGoods_Id());
            if(result3 != null){
                Integer result4 = purchaseMapper.updateGoodsSave(item.getWarehouse_Id(),item.getGoods_Id(),item.getPurchase_Number(),result3.getGoods_Save_Num());
            }else {
                Integer result5 = purchaseMapper.insertGoodsSave(item.getWarehouse_Id(),item.getGoods_Id(),item.getPurchase_Number());
            }
        }
            r.setCode(200);
            r.setMsg("添加成功");
            r.setData(null);

        return r;
    }

    @Override
    public R auditReceiptPurchase(Map map) {
        Integer result = purchaseMapper.auditReceiptPurchase(map);
        if(result != 0) {
            r.setCode(200);
            r.setMsg("审核成功");
            r.setData(null);
        }
        return r;
    }


}
